package com.sailfish.springbootdemo.dao.db2;

import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.pojo.db2.PcbSpecialProcessData;
import com.sailfish.springbootdemo.pojo.db2.PcbSpecialProcessVersion;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbSpecialProcessVersionDao {

    @Results(id = "pcbSpecialProcessVersionResultMap", value = {
            @Result(column = "pcb_special_process_version_id", property = "pcbSpecialProcessVersionId", jdbcType = JdbcType.INTEGER),
            @Result(column = "version", property = "version", jdbcType = JdbcType.VARCHAR),
            @Result(column = "active", property = "active", jdbcType = JdbcType.INTEGER),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_user", property = "createUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "update_user", property = "updateUser", jdbcType = JdbcType.VARCHAR),
    })

    /*
     * 查询单个
     */
    @Select("<script> " +
            "select *" +
            " from " + DB2TableName.PCB_SPECIAL_PROCESS_VERSION +
            "<where> " +
            "<if test='id != null'>" +
            " and pcb_special_process_version_id = #{id}" +
            "</if>" +
            "</where> " +
            " order by sort" +
            "</script>")
    List<PcbSpecialProcessVersion> getById(@Param("id") Integer id);

    @Select("<script> " +
            "select *" +
            " from " + DB2TableName.PCB_SPECIAL_PROCESS_VERSION +
            "<where> " +
            "<if test='version != null'>" +
            " and version = #{version}" +
            "</if>" +
            "<if test='active != null'>" +
            " and active = #{active}" +
            "</if>" +
            "</where> " +
            " order by sort" +
            "</script>")
    List<PcbSpecialProcessVersion> getByLimit(@Param("version") String version, @Param("active") Integer active);

    @Select("<script> " +
            "select *" +
            " from " + DB2TableName.PCB_SPECIAL_PROCESS_VERSION +
            " ORDER BY CAST(version AS DECIMAL(9,4)) desc,sort" +
            "</script>")
    List<PcbSpecialProcessVersion> getByVersionOrder();

    @Insert("insert into " + DB2TableName.PCB_SPECIAL_PROCESS_VERSION + "(version, active, remark, create_time, create_user, update_time, update_user, status, sort) values" +
            " (#{version}, #{active}, #{remark}, #{createTime}, #{createUser}, #{updateTime}, #{updateUser}, #{status}, #{sort})")
    // 显式设置主键
    @Options(useGeneratedKeys = true, keyProperty = "pcbSpecialProcessVersionId")
    Integer insert(PcbSpecialProcessVersion param);

    /*
     *通过主键删除
     */
    @Delete("delete from " + DB2TableName.PCB_SPECIAL_PROCESS_VERSION + " where pcb_special_process_version_id = #{id} ")
    Integer delete(@Param("id") Integer id);

    /*
     *通过主键修改数据
     */
    @Update("<script>" +
            "update " + DB2TableName.PCB_SPECIAL_PROCESS_VERSION +
            "<set> " +
            "<if test='version != null and version != \"\"'>" +
            "version = #{version}," +
            "</if>" +
            "<if test='active != null'>" +
            "active = #{active}," +
            "</if>" +
            "<if test='remark != null and remark != \"\"'>" +
            "remark = #{remark}," +
            "</if>" +
            "<if test='createUser != null '>" +
            "create_user = #{createUser}," +
            "</if>" +
            "<if test='createTime != null '>" +
            "create_time = #{createTime}," +
            "</if>" +
            "<if test='updateUser != null '>" +
            "update_user =#{updateUser}," +
            "</if>" +
            "<if test='updateTime != null '>" +
            "update_time = #{updateTime}," +
            "</if>" +
            "<if test='status != null '>" +
            "status = #{status}," +
            "</if>" +
            "<if test='sort != null '>" +
            "sort = #{sort} " +
            "</if>" +
            "</set> " +
            "where pcb_special_process_version_id = #{pcb_special_process_version_id}" +
            "</script>")
    Integer update(PcbSpecialProcessVersion param);
}
